package timbap.visualizer.api.plugin.container;

import timbap.visualizer.api.model.RecordItem;
import timbap.visualizer.api.plugin.Plugin;
import timbap.visualizer.api.plugin.recorditems.RecordItemsPlugin;

import java.util.ArrayList;
import java.util.List;

/**
 * The <code>AbstractMixedPluginFork</code> provides a base
 * implementation for plugin forks that contain both record items
 * drawing plugins and plugins with no record item specific drawing.
 *
 * It delegates the <code>draw()</code> call to calls of the
 * <code>drawRecordItem(RecordItem)</code> method for every <code>RecordItem</code>,
 * if the child plugin is a <code>RecordItemsPlugin</code>,
 * otherwise directly to a call of the <code>draw()</code> method.
 *
 * @author  Andreas Pabst
 * @see     timbap.visualizer.api.plugin.recorditems.RecordItemsPlugin
 * @see     timbap.visualizer.api.plugin.container.AbstractPluginFork
 * @see     timbap.visualizer.api.plugin.container.AbstractGlobalPluginFork
 * @see     AbstractRecordItemsPluginFork
 */
public abstract class AbstractMixedPluginFork extends AbstractPluginFork {

	public void draw() {

    	List<RecordItemsPlugin> tRecordItemsPlugins = new ArrayList<RecordItemsPlugin>();
    	for (Plugin tPlugin: getPlugins()) {
			if (tPlugin instanceof RecordItemsPlugin) {
				tRecordItemsPlugins.add((RecordItemsPlugin)tPlugin);
			} else {
				tPlugin.draw();
			}
    	}
	
    	for (RecordItem tRecordItem: getContext().getRecordItemManager().getRecordItems()) {
    		for (RecordItemsPlugin tRecordItemsVisualizingPlugin : tRecordItemsPlugins) {
                if (tRecordItemsVisualizingPlugin.isVisualizationNecessary(tRecordItem)) {
    			    tRecordItemsVisualizingPlugin.drawRecordItem(tRecordItem);
    			}
    		}
    	}
	}

}
